package com.doudoukeji.service.Activity.my;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.doudoukeji.service.Activity.Login.loginActivity;
import com.doudoukeji.service.Bean.HospitalLitapal;
import com.doudoukeji.service.Bean.ServiceLitapal;
import com.doudoukeji.service.Bean.Token;

import com.doudoukeji.service.Dialog.LoginOutDialog;
import com.doudoukeji.service.R;
import com.doudoukeji.service.Utils.DataCleanManager;
import com.doudoukeji.service.Utils.LoaderImage;

import com.sanhai.android.mvp.EduActivity;

import org.apache.commons.lang3.StringUtils;


/**
 * @version 2.0
 * @类描述： 我的界面
 * @项目名称：ddgh
 * @类名称： MyActivity
 * @包名称： com.doudoukeji.service.Activity.my
 * @创建人：zhanjunjie
 * @创建时间：2016/3/9 14:12
 * @修改人：
 * @修改时间：2016/3/9 14:12
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

public class MyActivity extends EduActivity implements MyView {

    private TextView mTvName, mTvPhone, mTVService, mTvhospital;
    private ImageView mImgfirend;
    private Button mBtnLoginOut;

    //数据
    private MyPresenter presenter;

    private LoaderImage loaderImage;


    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_mine);
    }

    @Override
    public void init() {
        loaderImage = new LoaderImage(getApplicationContext());
        mTvName = (TextView) findViewById(R.id.tv_user_name);
        mTvPhone = (TextView) findViewById(R.id.tv_user_phone);
        mTVService = (TextView) findViewById(R.id.tv_user_project);
        mTvhospital = (TextView) findViewById(R.id.tv_user_infirmary);
        mBtnLoginOut = (Button) findViewById(R.id.btn_loginout);
        mImgfirend = (ImageView) findViewById(R.id.img_friend);
        mBtnLoginOut.setOnClickListener(this);
        initData();

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        presenter = new MyPresenter(this, this);
        presenter.getMyInfo();
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId()) {
            case R.id.btn_loginout:
                LoginOutDialog  loginOutDialog  = new LoginOutDialog(this,presenter);
                loginOutDialog.show();
                break;
        }
    }


    @Override
    public void getMyInfo(MyBean myBean) {
        if (myBean != null) {
            mTvName.setText(myBean.getName());
            mTvPhone.setText(myBean.getPhone());

            if (StringUtils.isNotBlank(Token.getServices())){
                if (StringUtils.isNotBlank(ServiceLitapal.getServiceIdName(Token.getServices()))) {
                    mTVService.setText(ServiceLitapal.getServiceIdName(Token.getServices()).substring(0, ServiceLitapal.getServiceIdName(Token.getServices()).lastIndexOf(",")));
                }
            }else{
                mTVService.setText("请选择服务项目");
            }

            if (StringUtils.isNotBlank(Token.getHospitals())){
                if (StringUtils.isNotBlank(HospitalLitapal.getHospitalIdName(Token.getHospitals()))) {
                    mTvhospital.setText(HospitalLitapal.getHospitalIdName(Token.getHospitals()).substring(0, HospitalLitapal.getHospitalIdName(Token.getHospitals()).lastIndexOf(",")));
                }
            }else{
                mTvhospital.setText("请选择医院");
            }

            loaderImage.load(mImgfirend, myBean.getCodeImage());
        }
    }

    @Override
    public void getfail() {
        showToastMessage("请求失败");
    }

    @Override
    public void loginoutok() {
        DataCleanManager.cleanExternalCache(getApplicationContext());
        DataCleanManager.cleanInternalCache(getApplicationContext());
//        SharedPreferencesUtils.setParam(MyActivity.this, "READ", "0");
        LoaderImage.clearCache();
        Token.logout();
        Intent intent = new Intent(this, loginActivity.class);
        startActivity(intent);
        finish();
    }



}
